package arena

// NewArena 创建竞技场实例
func NewArena(uid string) *Arena {
	return &Arena{UID: uid}
}
func (a Arena) EnterArena() (map[string]interface{}, error) {
	arenaLogic := ArenaLogic{}
	/*if !common.IsFeatureOpen(a.UID, "arena") {
		return nil, errors.New("arena feature is locked")
	}
	*/
	// 获取竞技场数据
	arenaData, err := arenaLogic.GetInfo(a.UID)
	if err != nil {
		return nil, err
	}

	// 设置 session
	//common.SetSession(a.UID, "global.arenaId", 1)

	// 获取战斗信息和广播
	arenaData["arena_msg"] = arenaLogic.GetMsg(a.UID)
	//arenaData.Broadcast = models.GetBroadcast()
	//
	//arenaData.ActivityBeginTime = models.GetArenaActivityStartTime()
	//arenaData.ActivityEndTime = models.GetArenaActivityEndTime()
	//arenaData.ActiveRate = models.GetArenaActivityRate()

	return arenaData, nil
}

// 获取竞技场信息
func (a *Arena) GetInfo() (map[string]interface{}, error) {
	//if !common.IsFeatureOpen(a.UID, "arena") {
	//	return nil, errors.New("arena feature is locked")
	//}
	arenaLogic := ArenaLogic{}
	arenaData, err := arenaLogic.GetInfo(a.UID)
	if err != nil {
		return nil, err
	}

	//arenaData.ActivityBeginTime = models.GetArenaActivityStartTime()
	//arenaData.ActivityEndTime = models.GetArenaActivityEndTime()
	//arenaData.ActiveRate = models.GetArenaActivityRate()

	return arenaData, nil
}

// 挑战对手
func (a *Arena) Challenge(position int, atkedUID string, buyAddedChallengeNum int, isClearCdTime bool) (map[string]interface{}, error) {
	//if !common.IsFeatureOpen(a.UID, "arena") {
	//	return nil, errors.New("arena feature is locked")
	//}
	arenaLogic := ArenaLogic{}
	challenge, err := arenaLogic.Challenge(a.UID, atkedUID, position, buyAddedChallengeNum, isClearCdTime)
	if err != nil {
		return nil, err
	}

	// 更新任务进度
	//common.UpdateDailyTask(a.UID, "arena_challenge")
	//common.AddArenaAttackTimes(a.UID)

	return challenge, nil
}

// jjc商店
func (a *Arena) ArenaShop() (map[string]interface{}, error) {
	//if !common.IsFeatureOpen(a.UID, "arena") {
	//	return nil, errors.New("arena feature is locked")
	//}
	arenaLogic := ArenaLogic{}
	cost := arenaLogic.Shop(a.UID)

	return map[string]interface{}{"shop_list": cost}, nil
}

// 购买jjc商店
func (a *Arena) BuyArenaShop(itemId int) (map[string]interface{}, error) {
	//if !common.IsFeatureOpen(a.UID, "arena") {
	//	return nil, errors.New("arena feature is locked")
	//}
	arenaLogic := ArenaLogic{}
	return arenaLogic.BuyItem(a.UID, itemId)
}

// 购买额外挑战次数
func (a *Arena) BuyAddedChallenge(num int) (map[string]interface{}, error) {
	//if !common.IsFeatureOpen(a.UID, "arena") {
	//	return nil, errors.New("arena feature is locked")
	//}
	arenaLogic := ArenaLogic{}
	cost, err := arenaLogic.BuyAddedChallenge(a.UID, num)
	if err != nil {
		return nil, err
	}

	return cost, nil
}

// 获取jjc奖励
func (a *Arena) GetPositionReward() (map[string]interface{}, error) {
	//if !common.IsFeatureOpen(a.UID, "arena") {
	//	return nil, errors.New("arena feature is locked")
	//}
	arenaLogic := ArenaLogic{}
	reward, err := arenaLogic.GetPositionReward(a.UID)
	if err != nil {
		return nil, err
	}

	return reward, nil
}
